﻿using Google.GData.AccessControl;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PW_kalendarz
{
    class CalendarHelper
    {
        public static void addUser(String name)
        {

            CalendarService service = new CalendarService("pw-kalendarz-testy");
            service.setUserCredentials("kontotestowex@gmail.com", "haslotest");

            AclEntry entry = new AclEntry();
            entry.Scope = new AclScope();
            entry.Scope.Type = AclScope.SCOPE_USER;
            entry.Scope.Value = name;

            entry.Role = AclRole.ACL_CALENDAR_EDITOR;

            Uri aclUri = new Uri("https://www.google.com/calendar/feeds/kontotestowex@gmail.com/acl/full");
            try
            {
                AclEntry insertedEntry = service.Insert(aclUri, entry) as AclEntry;
            }
            catch
            {
                MessageBox.Show("Użytkownik " + name + " ma już uprawnienia");
            }
        }

        public static void addEntry(String entryName, DateTime start, DateTime end, CalendarService service)
        {

            EventEntry entry = new EventEntry();

            // Set the title and content of the entry.
            entry.Title.Text = entryName;
            entry.Content.Content = "Opis wydarzenia";

            // Set a location for the event.
            Where eventLocation = new Where();
            eventLocation.ValueString = "Miejsce wydarzenia";
            entry.Locations.Add(eventLocation);

            When eventTime = new When(start, end);
            entry.Times.Add(eventTime);

            Uri postUri = new Uri("https://www.google.com/calendar/feeds/kontotestowex@gmail.com/private/full");

            // Send the request and receive the response:
            AtomEntry insertedEntry = service.Insert(postUri, entry);
        }

        public static List<CustomEvent> getEntries(String eventName)
        {
            List<CustomEvent> eventList = new List<CustomEvent>();
            CalendarService service = new CalendarService("pw-kalendarz-testy");
            service.setUserCredentials("kontotestowex@gmail.com", "haslotest");

            EventQuery myQuery = new EventQuery("https://www.google.com/calendar/feeds/default/private/full");
            myQuery.Query = eventName;
            EventFeed myResultsFeed = service.Query(myQuery);
            foreach (var item in myResultsFeed.Entries)
            {
                EventEntry tempEvent = (EventEntry)item;

                var whens = tempEvent.Times;
                var startTime = whens[0].StartTime;
                var endTime = whens[0].EndTime;

                int date = Convert.ToInt32(startTime.Year.ToString() + startTime.Month.ToString() + startTime.Day.ToString());
                int startHour = Convert.ToInt32(startTime.Hour.ToString() + startTime.Minute.ToString());
                int endHour = Convert.ToInt32(endTime.Hour.ToString() + endTime.Minute.ToString());
                CustomEvent tempCustomEvent = new CustomEvent(tempEvent.Title.Text, tempEvent.Authors.ElementAt(0).Email, date, startHour, endHour);

                eventList.Add(tempCustomEvent);

            }
            return eventList;
        }
    }
}
